home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Freeware / CharMap / Sources / acl.c next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  8.9 KB  |  338 lines

  1. /***************************************************************
  2. ** acl.c: Contain managment of ASL requesters, clipboard and  **
  3. **        locale.library support. Written by T.Pierron.       **
  4. **        10-Dec-1999                                         **
  5. ***************************************************************/
  6.  
  7.  
  8. #include <intuition/intuition.h>
  9. #include <intuition/intuitionbase.h>
  10. #include <libraries/commodities.h>
  11. #include <libraries/gadtools.h>
  12. #include <libraries/asl.h>
  13. #include <dos/dosasl.h>
  14. #include <exec/io.h>
  15. #include <devices/clipboard.h>
  16. #include <libraries/locale.h>
  17. #include <string.h>
  18.  
  19. #include "cmap.h"
  20. #define    CATCOMP_STRINGS
  21. #define  CATCOMP_NUMBERS
  22. #include "cmap_strings.h"
  23.  
  24.  
  25. extern struct IntuitionBase *IntuitionBase;
  26. extern struct AslBase *AslBase;
  27. extern struct Window *window;
  28. struct FontRequester    *FR=NULL;
  29.  
  30. /* ASL Font requester tags: */
  31. ULONG FileTags[] = {
  32.     ASLFO_Window,NULL,
  33.     ASLFO_Screen,NULL,
  34.     ASLFO_SleepWindow,TRUE,
  35.     ASLFO_TitleText,(ULONG) MSG_ASLTITLE_STR,
  36.     ASLFO_MaxHeight,255,
  37.     TAG_DONE
  38. };
  39.  
  40. /*** Try to open a ASL font requester: ***/
  41. struct TTextAttr *open_asl( void )
  42. {
  43.     FileTags[1] = (ULONG) window;
  44.     FileTags[3] = (ULONG) IntuitionBase->ActiveScreen;
  45.  
  46.     if(!FR)
  47.         if( !(FR = (void *) AllocAslRequest(ASL_FontRequest,NULL)) ) {
  48.             puts(Errors[6]);
  49.             return NULL;
  50.         }
  51.  
  52.     if( AslRequest((APTR) FR,FileTags) )
  53.         return &FR->fo_TAttr;
  54.     else
  55.         /* User hit cancel or close gadget! */
  56.         return NULL;
  57. }
  58.  
  59. struct EasyStruct FontReq = {
  60.     sizeof(struct EasyStruct),0,
  61.     NULL,    NULL,    NULL
  62. };
  63. extern struct EasyStruct Request;
  64.  
  65. BYTE avert( STRPTR msg )
  66. {
  67.     if( FontReq.es_Title == NULL )
  68.     {
  69.         FontReq.es_Title = Request.es_Title;
  70.         FontReq.es_TextFormat = msg;
  71.         if( AslBase == NULL )
  72.             FontReq.es_GadgetFormat = strchr( FontReq.es_GadgetFormat, '|' ) + 1;
  73.     }
  74.     /* Something goes wrong: the font seems too big, so inquiring for a more **
  75.     ** convenient font... only if ASL library is present, otherwise quit.    */
  76.     return (BYTE) EasyRequestArgs( window, &FontReq, NULL, NULL );
  77. }
  78.  
  79. /*** Convert a geometry string, describing windows positions: ***/
  80. void Parse_geometry(UBYTE *p)
  81. {
  82.     extern WORD Offset[];
  83.     BYTE sign=FALSE;
  84.     WORD *nb;
  85.  
  86.     for(nb=Offset; ; p++)
  87.     {
  88.         /* Sorry for this crap routine... */
  89.         if(*p>='0' && *p<='9')
  90.             for(*nb=0; *p>='0' && *p<='9'; *nb*=10, *nb += *p++ - '0');
  91.  
  92.         switch(*p)
  93.         {
  94.             case '-': sign=TRUE; break;
  95.             case '+': break;
  96.             case 0:
  97.             case ',': if(sign) *nb = - *nb; nb++; sign=FALSE;
  98.                       if(*p && nb-Offset<4) break;
  99.             default:  return;
  100.         }
  101.     }
  102. }
  103.  
  104. /*** Adjust position to what user wants: ***/
  105. WORD Adjust_pos(WORD pos, WORD max, WORD len)
  106. {
  107.     /* if 0x7FFF center between 0 and max: */
  108.     if(pos==0x7FFF) return (WORD)(max-len>>1);
  109.     /* if neg, align pos to max: */
  110.     if(pos<0) pos+=max-len+1;
  111.     if(pos<0) return 0;
  112.     if(pos+len>max) return (WORD)(max-len);
  113.     return pos;
  114. }
  115.  
  116. /****************************************************************
  117. ** I/O procedures handling Clipboard.device. Taken from an ex- **
  118. ** ample of RKM, modify and optimize by the Author.            **
  119. ****************************************************************/
  120.  
  121.  
  122. #define MAKE_ID(a,b,c,d) ((a<<24L) | (b<<16L) | (c<<8L) | d)
  123.  
  124. #define ID_FORM MAKE_ID('F','O','R','M')
  125. #define ID_FTXT MAKE_ID('F','T','X','T')
  126. #define ID_CHRS MAKE_ID('C','H','R','S')
  127.  
  128. /* prototypes */
  129. struct IOClipReq    *CBOpen            ( ULONG );
  130. void                    CBClose            (struct IOClipReq *);
  131. int                    CBWriteFTXT        (struct IOClipReq *, UBYTE *);
  132. int                    CBQueryFTXT        (struct IOClipReq *);
  133. void                    CBReadCHRS        (struct IOClipReq *, UBYTE *,WORD Max);
  134. void                    CBReadDone        (struct IOClipReq *);
  135.  
  136.  
  137. /*** Try to open the clipboard: ***/
  138. struct IOClipReq *CBOpen(ULONG unit)
  139. {
  140.     struct MsgPort *mp;
  141.     struct IOStdReq *ior = NULL;
  142.  
  143.     if( (mp = (void *) CreatePort(0L,0L)) &&
  144.         (ior=(struct IOStdReq *)CreateExtIO(mp,sizeof(struct IOClipReq))) &&
  145.         (!OpenDevice("clipboard.device",unit,ior,0L)) )
  146.         return (struct IOClipReq *)ior;
  147.  
  148.     if(ior) DeleteExtIO(ior);
  149.     if(mp)  DeletePort(mp);
  150.     return NULL;
  151. }
  152.  
  153. /*** Try to close it: ***/
  154. void CBClose(struct IOClipReq *ior)
  155. {
  156.     struct MsgPort *mp = ior->io_Message.mn_ReplyPort;
  157.  
  158.     CloseDevice((struct IOStdReq *)ior);
  159.     DeleteExtIO((struct IOStdReq *)ior);
  160.     DeletePort(mp);
  161. }
  162.  
  163. /** Nasty, but required for strict-ANSI compiler **/
  164. typedef union
  165. {
  166.     char  *ptr8;
  167.     short *ptr16;
  168.     long  *ptr32;
  169. }    PTR;
  170. UBYTE Buffer[20];
  171.  
  172. /*** Write a string of text to the clipboard.device: ***/
  173. int CBWriteFTXT(struct IOClipReq *ior,UBYTE *string)
  174. {
  175.     PTR   p;
  176.     ULONG slen;
  177.     char  odd;
  178.  
  179.     if((slen = strlen(string))==0)    return FALSE;
  180.     odd = slen&1;                            /* pad byte */
  181.  
  182.     /* initial set-up for Offset, Error, and ClipID */
  183.     ior->io_Offset = 0;
  184.     ior->io_Error  = 0;
  185.     ior->io_ClipID = 0;
  186.     p.ptr8 = Buffer;
  187.  
  188.     /* Create the IFF header information */
  189.     *(p.ptr32)++ = ID_FORM;                /* "FORM"             */
  190.     *(p.ptr32)++ = slen+12+odd;        /* + "[size]FTXTCHRS" */
  191.     *(p.ptr32)++ = ID_FTXT;                /* "FTXT"             */
  192.     *(p.ptr32)++ = ID_CHRS;                /* "CHRS"             */
  193.     *(p.ptr32)++ = slen;                    /* string length      */
  194.  
  195.     /* Write header to clipboard: */
  196.     ior->io_Data    = (STRPTR)Buffer;
  197.     ior->io_Length  = (UBYTE *)p.ptr8-Buffer;
  198.     ior->io_Command = CMD_WRITE;
  199.     DoIO( (struct IORequest *) ior);
  200.  
  201.     /* Write the content of string: */
  202.     ior->io_Data    = (STRPTR)string;
  203.     ior->io_Length  = slen+odd;
  204.     DoIO( (struct IORequest *) ior);
  205.  
  206.     /* Tell the clipboard that we are done writing: */
  207.     ior->io_Command=CMD_UPDATE;
  208.     DoIO( (struct IORequest *) ior);
  209.  
  210.     /* Check if io_Error was set by any of the preceding IO requests */
  211.     return ior->io_Error ? FALSE : TRUE;
  212. }
  213.  
  214. #define Tab(Buf,Type,n)        ((Type *)Buf)[n]
  215.  
  216. /*** Check if there is TXT in the clipboard: ***/
  217. int CBQueryFTXT(struct IOClipReq *ior)
  218. {
  219.     /* initial set-up for Offset, Error, and ClipID */
  220.     ior->io_Offset = 0;
  221.     ior->io_Error  = 0;
  222.     ior->io_ClipID = 0;
  223.  
  224.     /* Look for "FORM[size]FTXT" */
  225.     ior->io_Command = CMD_READ;
  226.     ior->io_Data    = (STRPTR)Buffer;
  227.     ior->io_Length  = 12;
  228.     DoIO( (struct IORequest *) ior);
  229.  
  230.     if( (ior->io_Actual == 12L) &&                    /* Do we have at least 12 bytes ? */
  231.         (Tab(Buffer,ULONG,0) == ID_FORM) &&        /* Does it starts with "FORM" ? */
  232.         (Tab(Buffer,ULONG,2) == ID_FTXT) )            /* Is it "FTXT" ? */
  233.         return TRUE;
  234.  
  235.     /* It's not "FORM[size]FTXT", so tell clipboard we are done */
  236.     CBReadDone(ior);
  237.     return FALSE;
  238. }
  239.  
  240.  
  241. /*** Reads the next CHRS chunk from clipboard ***/
  242. void CBReadCHRS(struct IOClipReq *ior,UBYTE *buf, WORD Max)
  243. {
  244.     ULONG size;
  245.  
  246.     /* Find next CHRS chunk */
  247.     ior->io_Command = CMD_READ;
  248.     ior->io_Data    = (STRPTR)buf;
  249.     ior->io_Length  = 8L;
  250.     for(;;)
  251.     {
  252.         /* Read the chunk ID and its length: */
  253.         DoIO( (struct IORequest *) ior);
  254.  
  255.         /* Have get enough data ? */
  256.         if (ior->io_Actual != 8) break;
  257.  
  258.         /* Get buffer size, and pad it if odd: */
  259.         size = Tab(buf,ULONG,1);
  260.         if(size & 1) size++;
  261.  
  262.         /* Is it a CHRS chunk ? */
  263.         if(Tab(buf,ULONG,0) == ID_CHRS)
  264.         {
  265.             if(size >= Max) size=Max-1;
  266.             ior->io_Length  = size;
  267.  
  268.             DoIO( (struct IOStdReq *) ior);
  269.             buf[size] = 0;
  270.             break;
  271.         }
  272.         /* If not, skip to next chunk */
  273.         else
  274.             ior->io_Offset += size;
  275.     }
  276.     CBReadDone(ior);
  277. }
  278.  
  279. /*** Tell clipboard we are done reading ***/
  280. void CBReadDone(struct IOClipReq *ior)
  281. {
  282.     ior->io_Command = CMD_READ;
  283.     ior->io_Data    = (STRPTR)Buffer;
  284.     ior->io_Length  = sizeof(Buffer)-2;
  285.  
  286.     /* falls through immediately if io_Actual == 0 */
  287.     while (ior->io_Actual)
  288.         if (DoIO( (struct IORequest *) ior)) break;
  289. }
  290.  
  291. /**********************************************
  292. **                Locale.library support                 **
  293. **********************************************/
  294.  
  295.  
  296. /* All strings affected: */
  297. extern UBYTE *GadTxt[],*popmsg[];
  298. extern struct NewMenu newmenu[];
  299. extern struct EasyStruct Request;
  300. extern struct NewBroker newbroker;
  301.  
  302. struct Vars {
  303.     UBYTE **msg;            /* Message to change */
  304.     UBYTE nb;                /* Nb contiguous msg to change */
  305.     WORD  size;                /* Size of contiguous memory */
  306.     WORD    NumStr;            /* Id string from catalog */
  307. } TabVars[]={
  308.     { GadTxt+3,            4,  sizeof(STRPTR),         MSG_COPY      },
  309.     { Errors,              9,  sizeof(STRPTR),         MSG_BADOS     },
  310.     { popmsg,              4,  sizeof(STRPTR),         MSG_CODE      },
  311.     { &newmenu[0].nm_Label,  17, sizeof(struct NewMenu), MSG_MENUTITLE },
  312.     { &Request.es_Title,   3,  sizeof(STRPTR),         MSG_ABOUT     },
  313.     { &newbroker.nb_Title, 2,  sizeof(STRPTR),         MSG_DESCLINE1 },
  314. };
  315.  
  316. void *catalog=NULL;
  317.  
  318. /*** Translate all messages of the application: ***/
  319. void Translate_srings( void )
  320. {
  321.     if(catalog=(void *)OpenCatalogA(NULL,"CharMap.catalog",NULL))
  322.     {
  323.         union { UBYTE **pptr; UBYTE *ptr; } mes;
  324.         struct Vars *p;
  325.         WORD n;
  326.         /* All necessary information is contained in our table: */
  327.         for(p=TabVars; p<TabVars+sizeof(TabVars)/sizeof(struct Vars); p++)
  328.             for(n=0,mes.pptr=p->msg; n<p->nb; mes.ptr += p->size )
  329.                 if(*mes.pptr!=NM_BARLABEL && *mes.pptr!=0)        /* Newmenus barlabel string */
  330.                     *mes.pptr = (UBYTE *) GetCatalogStr(catalog,p->NumStr++,*mes.pptr), n++;
  331.  
  332.         FileTags[7] = (ULONG) GetCatalogStr(catalog, MSG_ASLTITLE, FileTags[7]);
  333.         FontReq.es_GadgetFormat = (STRPTR) GetCatalogStr(catalog, MSG_CHOOSEQUIT, 
  334.                 FontReq.es_GadgetFormat);
  335.     }
  336. }
  337.  
  338.